import { readFileSync } from 'fs-extra';
import { join } from 'path';
import { createApp, App } from 'vue';
import { helper } from '../../Helper';
import { readSecretKey } from '../../encrypt';
const panelDataMap = new WeakMap<any, App>();

let view: {
    progress: number;
    createProgress: number;
    buildDir: string;
    buildOutDir: string;
    isProcessing: boolean;
    isRouterAlive: boolean;
    packageCfg: PackageCfg;
    bundles:{ [key: string]: BundleCfg };
    serverCfg :ServerCfg; 
    channelCfg :string; 
};

module.exports = Editor.Panel.define({
    listeners: {

    },
    template: readFileSync(join(__dirname, '../../../static/template/default/index.html'), 'utf-8'),
    style: readFileSync(join(__dirname, '../../../static/style/default/index.css'), 'utf-8'),
    $: {
        app: '#app'
    },
    methods: {
        updateCreateProgress(progress: number) {
            view.createProgress = progress;
        },
        updateDeployProgress(progress: number) {
            view.progress = progress;
        },
        onSetBuildDir(dest: string) {
            view.buildDir = dest;
            view.buildOutDir = helper.config.buildOutDir;
        },
        onSetProcess(isProcess: boolean) {
            view.isProcessing = isProcess;
        },
        onRefresh() {
            console.log("==============编辑器页面刷新============")
            view.isRouterAlive = false;
            view.isRouterAlive = true;
            view.bundles = helper.bundles;
            view.serverCfg = helper.config.serverCfg;
            view.channelCfg = helper.config.channelCfg;
            view.packageCfg = helper.config.packageCfg;
            //view.$nextTick(() => (view.isRouterAlive = true))
        }
    },
    ready() {
        helper.readCacheConfig();
        if (this.$.app) {
            const app = createApp({});
            //指定Vue3 自己定义控件跳过解析
            app.config.compilerOptions.isCustomElement = tag => tag.startsWith("ui-")
            app.component('view-content', {
                template: readFileSync(join(__dirname, '../../../static/template/vue/view.html'), 'utf-8'),
                data() {
                    console.log("Update  data " + helper.config.platform)
                    return {
                        versionName: helper.config.versionName,
                        versionCode: helper.config.versionCode,
                        bundles: helper.bundles,
                        buildDir: helper.config.buildDir,
                        buildOutDir: helper.config.buildOutDir,
                        packageCfg: (helper.config.packageCfg ? helper.config.packageCfg : helper.PackageCfgs[0]),
                        packageCfgs: helper.PackageCfgs,
                        serverCfg:helper.config.serverCfg,
                        serverCfgs:helper.ServerCfgs,
                        channelCfg:helper.config.channelCfg,
                        channelCfgs:helper.ChannelCfgs,
                        autoCreate: helper.config.autoCreate,
                        isEncrypt: helper.config.isEncrypt,
                        progress: 0,
                        createProgress: 0,
                        isProcessing: false,
                        platforms: ["android", "ios"],
                        platform: helper.config.platform,
                        enginePath: helper.config.enginePath,
                        isRouterAlive: true,
                        secretKey: ''
                    };
                },
                methods: {
                    onChangeServer(name:string){
                        helper.setServerCfg(name)
                        helper.saveConfig()
                    },
                    onChangeChannel(channel:string){
                        helper.setChannelCfg(channel)
                        helper.saveConfig()
                    },
                    onChangePackage(packageName: string) {
                        console.log("选择游戏配置：", packageName)
                        helper.setPackageCfg(packageName)
                        helper.saveConfig()
                    },
                    onChangePlatform(value: string) {
                        //console.log("onChangePlatform : ", value)
                        this.platform = value
                        helper.setplatform(value);
                        helper.saveConfig();
                        this.onRefresh();
                    },
                    onChangeVersionName(value: string) {
                        console.log("修改版本名称 : ", value)
                        value = value.trim();
                        // 验证value是否为'1.0.0'格式
                        if (!/^\d+\.\d+\.\d+$/.test(value)) {
                            return;
                        }
                        helper.config.versionName = value;
                        helper.config.packageCfg.versionName = value;
                        helper.saveConfig();
                    },
                    onChangeVersionCode(value: string) {
                        console.log("修改版本号 : ", value)
                        value = value.trim();
                        // 验证value是否为数字格式
                        if (!/^\d+$/.test(value)) {
                            return;
                        }
                        const code = parseInt(value);
                        console.log("修改版本号 : ", code)
                        helper.config.versionCode = code;
                        helper.config.packageCfg.versionCode = code;
                        helper.saveConfig();
                        helper.savePackageCfg();
                    },
                    // onChangeProjectPath(value: string) {
                    //     console.log("切换工程目录 : ", value)
                    //     helper.setProjectPath(value)
                    //     helper.saveConfig();
                    // },
                    onChangeAutoBuild(value: boolean) {
                        helper.config.autoCreate = value;
                        helper.saveConfig();
                    },

                    onChangeIsEncrypt(value: boolean) {
                        // this.isEncrypt = value;
                        helper.config.isEncrypt = value;
                        helper.saveConfig();
                    },
                    onChangeEnginePath(value: string) {
                        helper.config.enginePath = value;
                        helper.saveConfig();
                    },
                    onChangeAutoDeploy(value: boolean) {
                        helper.config.autoDeploy = value;
                        helper.saveConfig();
                    },
                    onSelectBundle(value: boolean, bundle: string) {
                        console.log(`选择 Bundle ${bundle}  是否选中 ${value}`)
                        helper.bundles[bundle].select = value;
                    },
                    onInputVersionOver(value:string,bundle:string){
                        console.log(`修改 ${bundle} 版号为：  ${value}`)
                        helper.onChangeVersion(bundle,value)
                    },
                    onSelectVersion(version: string, bundle: string) {
                        console.log(`Version:${version} dir:${bundle}`)
                        helper.bundles[bundle].version = version;
                    },
                    onCommitVersionToServer(bundle: string) {
                        console.log(`提交版本号：Bundle: ${bundle}  version:${helper.bundles[bundle].version}`, this)
                        helper.onCommitVersionToServer(helper.bundles[bundle])
                    },
                    onDeployToRemote(bundle: string) {
                        console.log("资源远程部署 Bundle: " , bundle)
                        helper.onDeployToOSS(bundle);
                        //helper.onDeployToRemote();
                    },
                    // onDeployToSVN(bundle: BundleCfg) {
                    //     console.log("上传资源到SVN Bundle: " , bundle)
                    //     helper.onDeployToSVN(bundle);
                    // },
                    onshowInExplorer(bundle:string){
                        console.log(bundle);
                        helper.onShowInExport(bundle)
                    },
                    //设置构建目录
                    onBuildDirConfirm(url: string) {
                        helper.setBuildPath(url);
                        helper.saveConfig();
                    },
                    //设置输出目录
                    onBuildOutDirConfirm(url: string) {
                        helper.setBuildOutPath(url);
                        helper.saveConfig();
                    },
                    onChangeHotupdateUrls(event: { target: HTMLSelectElement }) {
                        this.onInputUrlOver(event.target.value);
                    },
                    onUserLocalIP() {
                        let network = require("os").networkInterfaces();
                        let url = "";
                        Object.keys(network).forEach((key) => {
                            network[key].forEach((info: any) => {
                                if (info.family == "IPv4" && !info.internal) {
                                    url = info.address;
                                }
                            });
                        });
                        if (url.length > 0) {
                            url = "http://" + url + "/hotupdate";
                        }
                        this.onInputUrlOver(url);
                    },
                    onBuildAssets() {
                        helper.onBuildAssets();
                    },
                    //构建bundle
                    onBuildBundle() {
                        helper.onBuildAssetBundles(helper.config.isEncrypt);
                        // this.onRefresh();
                    },
                    onRefresh() {
                        console.log("刷新页面")
                        readSecretKey().then((key)=>{this.secretKey=key});
                        view.isRouterAlive = false;
                        this.$nextTick(() => (this.isRouterAlive = true))
                    },
                    //工程构建
                    onBuildPackage() {
                        helper.onBuildPackage();
                        //this.onRefresh();
                    },
                },
                created: function () {
                    view = this;
                    readSecretKey().then((key)=>{this.secretKey=key});
                },
                mounted: function () {
                }
            });
            app.mount(this.$.app);
            panelDataMap.set(this, app);
        }
    },
    beforeClose() { console.log("beforeClose---------------->") },
    close() { 
        console.log("close---------------->") 
        const app = panelDataMap.get(this);
        if (app) {
            app.unmount();
        }
    },
});
